
#ifndef _SYSTEMDATA_H_
#define _SYSTEMDATA_H_

#include <stdbool.h>
#include "sys_hc32f460.h"

#ifndef SOFT_VERSION
#define	SOFT_VERSION	"XG-D-V1.1.8"   			//24-4-30
#endif
#define XG_READ_ERR_LOG_FROM_TIME         1970
#define XG_READ_ERR_LOG_TO_TIME           2300

#define XG_STORE_HIST_DATA_LENGTH (sizeof(XgStoreHistoriCalData))
#define XG_ERR_LOG_STORE_ADDR   1016
typedef union{
	uint8_t byte[2];
	uint16_t value;
}union_uint16_t;

typedef enum{
	chg_wake_bit 	= 0x00,
	can_wake_bit 	= 0x01,
	rs485_wake_bit 	= 0x02,
	light_wake_bit 	= 0x03,
	key_wake_bit   	= 0x04,
	load_det_bit    = 0x05,
}WakeOptBit;

typedef union{
	struct{
		uint8_t CHG_WAKE		:1;//
		uint8_t CAN_WAKE		:1;//
		uint8_t RS485_WAKE		:1;//
		uint8_t LIGHT_WAKE		:1;//
		uint8_t KEY_WAKE		:1;//
		uint8_t LOAD_DET        :1;
	}bits;
	uint16_t byte;
}WakeupState_t;

//电池类型
typedef enum {
	sanyuanBat = 0x01,						//三元锂电池
	lstlBat    = 0x02,						//磷酸铁锂电池
	mslBat     = 0x03,						//锰酸锂电池
	gslBat     = 0x04,						//钴酸锂电池
	tslBat     = 0x05,						//钛酸锂电池
	nslBat     = 0x06,                      //钠锂电池
}XgBatType;

//电池状态
typedef enum {
	stdState   = 0x01,						//静置状态
	chgState   = 0x02,						//充电状态
	dsgState   = 0x03,						//放电状态
	slpState   = 0x04,						//休眠状态
	lpwState   = 0x05,						//低功耗状态
	errState   = 0x0f,						//故障状态
}XgBatState;

//使用模式
typedef enum{
	singlePackMode = 0x00,					//单组使用模式
	multiPackParallelMode = 0x01,			//多组并联使用模式
}XgApplyMode;					

typedef union {
	struct {
		uint16_t SW_CHG  		: 1;		//充电开关
		uint16_t SW_DSG  		: 1;		//放电开关
		uint16_t SW_PRE  		: 1;		//预充开关
		uint16_t SW_CHG_LI   	: 1;		//充电限流开关
		uint16_t SW_OTA  		: 1;		//OTA升级开关
		uint16_t SW_HT  		: 1;		//加热开关
		uint16_t RM		 		: 10;		//预留
	} bits;
	uint16_t stateByte;
} XgSwitchState;

typedef union {
	struct {
		uint16_t SW_CHG_EN 		: 1;		//充电允许
		uint16_t SW_DSG_EN 		: 1;		//放电允许
		uint16_t SW_BL_EN 		: 1;		//均衡允许
		uint16_t SW_HT_EN 		: 1;		//加热允许
		uint16_t RM		 		: 12;		//预留
	} bits;
	uint16_t stateByte;
} XgFunctionalState;

typedef union{
	struct{
		uint16_t rs485_lpwr_flag	:1;		//485低功耗状态
		uint16_t rs485_link			:1;		//485通讯状态(只要有485通讯数据，即为1)
		uint16_t rs485_obc_timeout	:1;		//485充电机连接超时状态(协议匹配时才为0)
		uint16_t rs485_obc_link		:1;		//485充电机连接,在超时状态为0时置1，在超时状态为1且累计一定时间时为0
		uint16_t can_lpwr_flag		:1;		//can低功耗状态
		uint16_t can_link			:1;		//can通讯状态(只要有can通讯数据，即为1)
		uint16_t can_obc_timeout	:1;		//can充电机连接超时状态(协议匹配时才为1)
		uint16_t can_obc_link		:1;		//can充电机连接,在超时状态为0时置1，在超时状态为1且累计一定时间时为0
		uint16_t chgCbtOutTimeState :1;     //换电柜连接超时状态
		uint16_t RM                 :7;
	}Bits;
	uint16_t byte;
}XgComLinkState;//外部通信连接状态
//----------------------------------------//
typedef union{
	struct{
		uint8_t packVolHgWarn     :1;    //总压高报警
		uint8_t packVolLowWarn    :1;    //总压低报警
		uint8_t cellVolDiffWarn   :1;    //单体电压差报警
		uint8_t socLowWarn        :1;    //容量低告警
		uint8_t RM                :4;    //预留
	}bit;
	uint8_t byte;
}XgVolWarn;


typedef struct {
	uint32_t cellHgVolWarn;     //单体高压告警
	uint32_t cellLowVolWarn;    //单体低压告警
	XgVolWarn volWarn;
}XgVolWarnByte;

//----------------------------------//
typedef union{
	struct{
		uint8_t packVolHgProt     :1;    //总压高保护
		uint8_t packVolLowProt    :1;    //总压低保护
		uint8_t cellVolDiffProt   :1;    //单体电压差保护
		uint8_t afeVolHgProt      :1;    //二级单体电压高保护
		uint8_t afeVolLowProt     :1;    //二级单体电压低保护
//		uint8_t afeBrokenLine     :1;    //断线保护
		uint8_t RM                :3;    //预留
	}bit;
	uint8_t byte;
}XgVolProt;


typedef struct {
	uint32_t cellHgVolProt;     //单体高压保护
	uint32_t cellLowVolProt;    //单体低压保护
	XgVolProt volProt;
}XgVolProtByte;
//-----------------------------------------------//
typedef union{
	struct{
		uint8_t envTempHgWarn     :1;    //环境高温告警
		uint8_t envTempLowWarn    :1;    //环境低温告警
		uint8_t chgTempHgWarn     :1;    //充电高温告警
		uint8_t chgTempLowWarn    :1;    //充电低温告警
		uint8_t disTempHgWarn     :1;    //放电高温告警
		uint8_t disTempLowWarn    :1;    //放电低温告警
		uint8_t mosTempHgWarn     :1;    //MOS高温告警
		uint8_t RM                :1;    //预留
	}bit;
	uint8_t byte;
}XgTempWarnByte;

typedef union{
	struct{
		uint8_t envTempHgProt     :1;    //环境高温保护
		uint8_t envTempLowProt    :1;    //环境低温保护
		uint8_t chgTempHgProt     :1;    //充电高温保护
		uint8_t chgTempLowProt    :1;    //充电低温保护
		uint8_t disTempHgProt     :1;    //放电高温保护
		uint8_t disTempLowProt    :1;    //放电低温保护
		uint8_t mosTempHgProt     :1;    //MOS高温保护
		uint8_t tempDiffProt      :1;    //温差保护
	}bit;
	uint8_t byte;
}XgTempProtByte;

typedef union{
	struct{
		uint8_t shortCurProt      :1;    //短路保护
		uint8_t disCur4Prot       :1;    //放电4级过流保护
		uint8_t disCur3Prot       :1;    //充电3级过流保护
		uint8_t disCur2Prot       :1;    //放电2级过流保护
		uint8_t disCur1Prot       :1;    //放电1级过流保护
		uint8_t chgCur3Prot       :1;    //充电3级过流保护
		uint8_t chgCur2Prot       :1;    //充电2级过流保护
		uint8_t chgCUr1Prot       :1;    //充电1级过流保护
	}bit;
	uint8_t byte;
}XgCurProtByte;

typedef union{
	struct{
		uint8_t comFaultProt      :1;    //通讯故障
		uint8_t IntFaultProt      :1;    //内部故障
		uint8_t RM                :6;    //预留
	}bit;
	uint8_t byte;
}XgAfeProtByte;


typedef union{
	struct{
		uint8_t key_check_enable        :1;    //钥匙检测
        uint8_t chag_pwr_prchg_enable   :1;    //换电开预充使能
		uint8_t RM                      :6;     //预留
	}bit;
	uint8_t byte;
}XgOtherParmByte;



//充电策略
typedef enum {
	paramCtrlPolicy  = 0x01,				//保护参数控制策略
	tempCtrlPolicy   = 0x02,				//温度区间控制策略
}XgChargeMap;

//故障记录代码
typedef enum {
	sbatOvCode      = 0x01,					//单体过压
	sbatUvCode      = 0x02,					//单体欠压
	afebatOvCode    = 0x03,                 //二级过压保护
	afebatUvCode    = 0x04,                 //二级欠压保护
	sbatDifCode     = 0x05,                 //单体压差
	packOvCode      = 0x06,					//总压过压
	packUvCode      = 0x07,					//总压欠压
	envOtCode       = 0x08,                 //环境过温
	envUtCode       = 0x09,                 //环境欠温	
	batChgOtCode    = 0x0a,					//充电高温
	batChgUtCode    = 0x0b,					//充电低温
	batDsgOtCode    = 0x0c,					//放电高温
	batDsgUtCode    = 0x0d,					//放电低温
	mosOtCode		= 0x0e,					//MOS管过温
	tempDiffCode    = 0x0f,                 //温差保护
	scdCode         = 0x10,					//短路	
	dsgOc4dCode     = 0x11,					//放电过流4	
	dsgOc3dCode     = 0x12,					//放电过流3
	dsgOc2dCode     = 0x13,					//放电过流2		
	dsgOcdCode      = 0x14,					//放电过流	
	chgOcd3Code     = 0x15,					//充电过流3
	chgOcd2Code     = 0x16,					//充电过流2
	chgOcdCode      = 0x17,					//充电过流
	afeComErrCode   = 0x18,					//模拟前端通信故障
	afeIntErrCode   = 0x19,					//模拟前端内部故障
}XgErrorCode;

//故障数据格式
typedef struct {
	uint16_t  errorCode;					//故障代码
	uint16_t occurTimes;					//故障发生次数
	uint8_t  latestOccurTime[6];			//最近一次发生时间
}XgErrorData;

//故障记录
typedef struct {
	uint8_t 		recordNum;				//故障数量
	XgErrorData   	recordData[25];			//故障数据
}XgErrRecord;

//运行数据
typedef struct {
	uint8_t			active_flag;			//激活标志
	uint8_t			low_pwr_flag;			//低功耗标志
	uint8_t			sleep_flag;				//休眠标志
	uint8_t			soc_init_flag;			//soc初始化标志
	XgBatState   	batState;				//电池状态
	uint8_t			poweroff_flag;	        //达到关机电压标志
	uint8_t   		batSoc;					//电池SOC
	uint8_t 		batSoh;					//电池SOH
	uint16_t  		packVol;				//电池总压	
	int32_t    		batCur;					//电池电流
	int32_t			batRawCur;				//原始采样电流
	int64_t			rmCap_mAms;				//电池剩余容量(单位：1mA.ms)
	uint64_t		realCap_mAms;			//电池实际容量(单位：1mA.ms)
	uint64_t		norCap_mAmS;			//电池标称容量(单位：1mA.ms)
	uint64_t		chgCap_mAmS;			//充电容量，用于计算循环次数
	uint16_t		batRawVol[32];			//单体电压原始采样值
	uint16_t  		batVol[32];				//单体电压
	int8_t   		mosTemp;				//MOS温度
	int8_t   		envTemp;				//环境温度
	int8_t   		tempOne;				//电池温度1
	int8_t   		tempTwo;				//电池温度2
	uint8_t			fullChgFlag;			//满充标志
	uint8_t         fullDsgFlag;          	//满放标志
	uint8_t			rmCapEmptyFlag;			//容量为空标志
	uint8_t   		batMaxNum;				//电压最高单体编号
	uint8_t   		batMinNum;				//电压最低单体编号
	uint16_t  		batMaxVol;				//单体最高电压
	uint16_t  		batMinVol;				//单体最低电压
	uint16_t		averageVol;				//单体平均电压
	uint16_t  		maxDiffVol;				//单体最大压差
	XgComLinkState	comLinkState;			//外部通信连接状态
	uint16_t  		chgReqVol;				//充电需求电压
	uint16_t  		chgReqCur;				//充电需求电流
	uint32_t    	balState;				//电芯均衡状态
	XgSwitchState 	sysSwState;				//系统开关状态
	XgFunctionalState 	funcSwState;		//系统功能状态
	WakeupState_t	wakeState;				//系统中断状态
	
	XgVolWarnByte   volWarnByte;			//电压告警状态
	XgVolProtByte   volProtByte;            //电压保护状态
	XgTempWarnByte  tempWarnByte;           //温度告警
	XgTempProtByte  tempProtByte;           //温度保护
	XgCurProtByte   curProtByte;            //过流保护
	XgAfeProtByte   afeProtByte;            //模拟前端故障
	uint8_t			rtcTime[6];				//年月日时分秒
	bool            curlimflag;				//充电限流需开标志位
//	uint64_t		sysStartTime_min;		//系统启动时间
	uint32_t		sysRunTime_min;			//系统工作时长
	
}SysRunData_t;

//配置数据
typedef struct{
	struct{//出厂参数
		uint8_t  		batType;				//电池类型	
		uint8_t  		batNum;					//电芯串数
		uint8_t  		hdSerial[12];			//硬件编号
		uint8_t  		BtNum[24];				//BT码  
		union_uint16_t 	normalCap;				//标称容量
		union_uint16_t 	realCap;				//实际容量
		union_uint16_t 	cycleTimes;				//循环次数
		union_uint16_t 	preChargeTime;			//预充时间
		union_uint16_t 	poweroff_vol;			//单体关机电压
		union_uint16_t 	wakeupCurrent;			//唤醒电流
		XgOtherParmByte otherParmByte;		    //其他参数，钥匙信号、换电预充
	}fctData;
	struct{//告警参数
		//单体过压
		union_uint16_t 	batVolHigh;
		//单体欠压
		union_uint16_t 	batVolLow;
		//单体压差
		union_uint16_t  batVolDiff;
		//总压过压
		union_uint16_t 	packVolHigh;
		//总压欠压
		union_uint16_t 	packVolLow;
		//MOS管过温
		int8_t  		mosTempHigh;
		//环境高温告警
		int8_t          envTempHigh;
		//环境低温告警
		int8_t          envTemplow;
		//充电过温
		int8_t  		chgTempHigh;
		//充电低温
		int8_t  		chgTempLow;
		//放电过温
		int8_t  		dsgTempHigh;
		//放电低温
		int8_t  		dsgTempLow;
		//低容量
		uint8_t         socLow;
	}warnData;
	struct{//保护参数
		//单体过压
		union_uint16_t 	batVolHigh;
		union_uint16_t 	batVolHighRecover;
		uint8_t  		batVolHighTime;
		//单体欠压
		union_uint16_t 	batVolLow;
		union_uint16_t 	batVolLowRecover;
		uint8_t  		batVolLowTime;
		
		union_uint16_t	sov_vol;	//单体过压
		union_uint16_t	suv_vol;	//单体欠压
		uint8_t	sov_suv_dly_s;	//超时		
		
		//单体压差保护
		union_uint16_t 	batVolDiff;
		union_uint16_t 	batVolDiffRecover;
		uint8_t  		batVolDiffTime;
		//总压过压
		union_uint16_t 	packVolHigh;
		union_uint16_t 	packVolHighRecover;
		uint8_t  		packVolHighTime;
		//总压欠压
		union_uint16_t 	packVolLow;
		union_uint16_t 	packVolLowRecover;
		uint8_t  		packVolLowTime;
		//MOS管过温
		int8_t  		mosTempHigh;
		int8_t  		mosTempHighRecover;
		uint8_t  		mosTempHighTime;
		//环境管过温
		int8_t  		envTempHigh;
		int8_t  		envTempHighRecover;
		uint8_t  		envTempHighTime;
		
		//环境低温
		int8_t  		envTempLow;
		int8_t  		envTempLowRecover;
		uint8_t  		envTempLowTime;

		//充电过温
		int8_t  		chgTempHigh;
		int8_t  		chgTempHighRecover;
		uint8_t  		chgTempHighTime;
		
		//充电低温
		int8_t  		chgTempLow;
		int8_t  		chgTempLowRecover;
		uint8_t  		chgTempLowTime;
		//放电过温
		int8_t  		dsgTempHigh;
		int8_t  		dsgTempHighRecover;
		uint8_t  		dsgTempHighTime;
		//放电低温
		int8_t  		dsgTempLow;
		int8_t  		dsgTempLowRecover;
		uint8_t  		dsgTempLowTime;

		//电池温差保护
		int8_t  		batTempDiff;
		int8_t  		batTempDiffRecover;
		uint8_t  		batTempDiffTime;
		//充电过流
		union_uint16_t 	chgOverCurrent;
		union_uint16_t 	chgOverCurRecoverTime;
		uint8_t  		chgOverCurrentTime;		
		//充电过流2
		union_uint16_t 	chgOverCurrent2;
		union_uint16_t 	chgOverCur2RecoverTime;
		uint8_t  		chgOverCurrent2Time;
		//充电过流3
		union_uint16_t 	chgOverCurrent3;
		union_uint16_t 	chgOverCur3RecoverTime;
		uint8_t  		chgOverCurrent3Time;		
		//放电过流
		union_uint16_t 	dsgOverCurrent;
		union_uint16_t 	dsgOverCurRecoverTime;
		uint8_t  		dsgOverCurrentTime;
		//放电过流2
		union_uint16_t 	dsgOverCurrent2;
		union_uint16_t 	dsgOverCur2RecoverTime;
		uint8_t  		dsgOverCurrent2Time;
		//放电过流3
		union_uint16_t 	dsgOverCurrent3;
		union_uint16_t 	dsgOverCur3RecoverTime;
		uint8_t  		dsgOverCurrent3Time;
		//放电过流4
		union_uint16_t 	dsgOverCurrent4;
		union_uint16_t 	dsgOverCur4RecoverTime;
		union_uint16_t  dsgOverCurrent4Time;
		//短路保护
		union_uint16_t  scdVol;
		union_uint16_t  scdVolTime;
		union_uint16_t  scdRecoverTime;
		//充电过流锁定次数
		union_uint16_t 	chgOverCurLockTimes;
		//放电过流锁定次数
		union_uint16_t  disOverCurLockTimes;
		//短路锁定次数
		union_uint16_t  scdLockTimes;
		
	}errorData;
	struct{//均衡参数		
		union_uint16_t 	openVol;
		union_uint16_t 	openDiffVol;
		union_uint16_t  endDiffVol;
		uint8_t		allowCtrl;
	}balData;
	struct{//校准系数
		uint16_t 	curRadio;		//电流校准系数，单位:0.001
		uint16_t 	lowAdcRadio;	//低侧ML5238单体采样校准系数，单位:0.001。
		uint16_t 	highAdcRadio;	//高侧ML5238单体采样校准系数，单位:0.001。对于16串一下电池组以及多路复用版BMS，此参数可忽略
	}caliData;
	struct{//至想项目参数
		uint8_t 		devAddr;//MODBUS地址
		union_uint16_t 	serialPortBaudRate;//串口波特率
	}zhixiangParam;
}SysConfData_t;


//历史数据保存存flash
typedef struct{
	uint8_t         record_time[6];  //故障时间记录
	uint8_t         batNum;         //电芯数目
	XgBatState      batState;        //电池状态	
	uint8_t         soc;             //剩余SOC
	uint16_t  	    packVol;	     //电池总压
	int32_t         batCurrent;	     //电池电流
	XgSwitchState   sysSwState;		 //系统开关状态
	uint8_t         tempNum;         //温度点数
	int8_t   		mosTemp;		 //MOS温度
	int8_t   		envTemp;		 //环境温度
	int8_t   		temp[10];		 //电池温度
	uint16_t  	    batVol[32];	     //单体电压		
	XgVolProtByte   volProtByte;     //电压保护状态
	XgTempProtByte  tempProtByte;    //温度保护
	XgCurProtByte   curProtByte;     //过流保护
	XgAfeProtByte   afeProtByte;     //模拟前端故障
}XgStoreHistoriCalData;


SysRunData_t *getSysRunData(void);
SysConfData_t *getSysConfData(void);
void saveSysConfData(void);
void saveSystemErrorRecord(void);
XgErrRecord *getSysErrRcdInfo(void);
void sysErrRcdClear(void);
void loadXgSystemParam(void);
#endif